/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Simulation;
import java.util.*;
/**
 *
 * @author Jeremy
 */
public class WeightedRandParam {
    
    private ArrayList<Integer> _values;
    private ArrayList<Double> _weights;
    
    public WeightedRandParam(int[] values, int[] weights) {
        _values = new ArrayList<Integer>();
        _weights = new ArrayList<Double>();
        for(int i = 0; i < values.length; i++) {
            _values.add(values[i]);
            _weights.add(new Double(weights[i]));
        }
        NormalizeWeights();
    }
    
        public WeightedRandParam(ArrayList<Integer> values, ArrayList<Integer> weights) {
        _values = values;
        _weights = new ArrayList<Double>();
        for(int i = 0; i < values.size(); i++) {
            _weights.add(new Double(weights.get(i)));
        }
        NormalizeWeights();
    }

    
    public int generateValue() {
        int target = new RandParam(0,100).IntValue();
        double start = 0, end = 0;
        for(int i = 0; i < _weights.size(); i++) {
            end = start + _weights.get(i);
            if(target >= start && target <= end) return _values.get(i);
            else start = end;
        }
        return 0;
    }
    
    private void NormalizeWeights() {
        double WeightTotal = 0;
        for(Double w: _weights) WeightTotal += w;
        double factor = 100/WeightTotal;
        for(Double w: _weights) w *= factor;
    }
    
}
